import { noVModelList, noTableAllowList, calculateItem, onlinePeculiarList } from '../generator/comConfig'
import { mapMutations } from 'vuex';
import {
  deepClone
} from '@/utils/formIndex'
let tempActiveData
export default{
    data(){
        return{
            // idGlobal: this.$store.state.designer.idGlobal,
            showTip: true,
            activeItem: {},
           
        }
    },
    
    methods: {
      ...mapMutations('designer',['setActiveTableItem', 'setActiveData', 'setActiveId']),
      activeFormItem(element) {
        this.setActiveData(element)
        this.setActiveId(element.config.formId)
      },
      cloneComponent(origin) {
        const clone = deepClone(origin)
        const config = clone.config
        // config.span = this.formConf.span // 生成代码时，会根据span做精简判断
        config.span = 24 // 生成代码时，会根据span做精简判断
        this.createIdAndKey(clone)
        tempActiveData = clone
   
        return tempActiveData
      },
      createIdAndKey(item, parent) {
            let idGlobal = this.$store.state.designer.idGlobal
            const config = item.config
            config.formId = ++idGlobal
            this.$store.commit('designer/setIdGlobal', config.formId)
            config.renderKey = +new Date() // 改变renderKey后可以实现强制更新组件
            if (config.layout === 'colFormItem') {
              if (!this.$store.getters.hasTable) {
                // 分割线和按钮不加vModel
                if (noVModelList.indexOf(config.pfKey) < 0 || config.isStorage == 2) {
                  item.vModel = this.toggleVmodelCase('')
                }
              } else {
                if (noVModelList.indexOf(config.pfKey) < 0 || config.isStorage != 2) {
                  item.vModel = ""
                }
              }
              if (parent && parent.vModel && parent.config.pfKey === 'table') {
                item.config.parentVModel = parent.vModel
              }
            } else if (config.layout === 'rowFormItem') {
              if (config.pfKey === 'table') {
                item.vModel = this.toggleVmodelCase(`${config.pfKey}Field${idGlobal}`);
              }
              config.componentName = `row${idGlobal}`
              !Array.isArray(config.children) && (config.children = [])
              // delete config.label // rowFormItem无需配置label属性
            }
            if (Array.isArray(config.children)) {
              config.children = config.children.map(childItem => this.createIdAndKey(childItem, item))
            }
            return item
      },
      toggleVmodelCase(str) {
        const dbType = this.dbType || ''
        if (dbType.toLowerCase() === 'Oracle'.toLowerCase() || dbType.toLowerCase() === 'DM'.toLowerCase()) {
          return str.toUpperCase()
        }
        if (dbType.toLowerCase() === 'PostgreSQL'.toLowerCase() || dbType.toLowerCase() === 'KingBaseES'.toLowerCase()) {
          return str.toLowerCase()
        }
        return str
      },

       //  阻止表格中嵌套行容器
      shouldClone(to, from, target, event, conf) {
        const targetConf = target._underlying_vm_
        const isRowContainer = conf.config.cmpType === 'common' && conf.config.rowType === 'layout'
        if (isRowContainer) return true
        if (conf.cmpType === 'custom') return false
        if (conf.config.rowType === 'table') {
          if (noTableAllowList.includes(targetConf.config.pfKey)) {
            // if (this.showTip) {
            //   this.$message.warning(`子表内暂不支持使用该组件`)
            //   this.showTip = false
            // }
            return false
          }
          if (targetConf.config.layout === 'rowFormItem') return false
          if (this.$store.getters.hasTable) {
            if (!conf.config.tableName) {
              if (this.showTip) {
                this.$message.warning(`子表请先关联数据表`)
                this.showTip = false
              }
              return false
            }
          }
          this.activeItem = targetConf
          this.setActiveTableItem(conf)
        }
        return true
      },
      }
}